package com.xywl.escort.view.farm.rank;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.xywl.escort.R;
import com.xywl.escort.adapter.LeaderBoardAdapter;
import com.xywl.escort.base.BaseFragment;
import com.xywl.escort.bean.RankingListResp;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import dagger.android.support.AndroidSupportInjection;
import timber.log.Timber;

public class SubscribeRankFragment extends BaseFragment implements SubscribeRankContract.View {
    private static final String ARG_PARAM1 = "param1";
    private String projectId;
    private LeaderBoardAdapter mAdapter;
    private List<RankingListResp.DataBean.ListBean> mDataBeanList = new ArrayList<>();
    private int pageNum = 1;
    private boolean isEnd = false;

    @Inject
    SubscribeRankContract.Presenter mPresenter;
    @BindView(R.id.recycler_leader_board)
    RecyclerView mRecyclerLeaderBoard;

    public SubscribeRankFragment() {
    }

    public static SubscribeRankFragment newInstance(String param1) {
        SubscribeRankFragment fragment = new SubscribeRankFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        AndroidSupportInjection.inject(this);
        super.onAttach(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            projectId = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_subscribe_rank;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        mAdapter = new LeaderBoardAdapter(R.layout.item_leader_board, mDataBeanList);
        LinearLayoutManager manager = new LinearLayoutManager(this.getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerLeaderBoard.setLayoutManager(manager);
        mRecyclerLeaderBoard.setAdapter(mAdapter);

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mRecyclerLeaderBoard.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (isEnd) {
                            //数据全部加载完毕
                            mAdapter.loadMoreEnd();
                        } else {
                            //成功获取更多数据
                            mPresenter.loadRankingList(projectId, pageNum);
                        }
                    }
                }, 200);
            }
        }, mRecyclerLeaderBoard);
    }

    @Override
    protected void initData() {
        mPresenter.takeView(this);
        mPresenter.loadRankingList(projectId, pageNum);
    }

    @Override
    public void loadRankingListSuccess(RankingListResp.DataBean dataBean) {
        if (pageNum == 1) {
            mDataBeanList.clear();
        }
        if (pageNum >= dataBean.pages) {
            isEnd = true;
        } else {
            isEnd = false;
            pageNum += 1;
        }
        mDataBeanList.addAll(dataBean.list);
        mAdapter.setNewData(mDataBeanList);
        mAdapter.loadMoreComplete();
    }

    @Override
    public void loadRankingListFailure() {
        Timber.e("loadRankingListFailure");
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.bindToLifecycle();
    }
}
